package com.basic.common;

import cn.hutool.core.util.StrUtil;
import com.sun.mail.imap.IMAPFolder;
import com.sun.mail.imap.IMAPStore;
import lombok.extern.slf4j.Slf4j;

import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.MimeMessage;

@Slf4j
public class EmailReceiver {
    public static void main(String[] args) throws Exception {
//        String user = "yue05@qhqy.xyz";
//        String password = "chuzhou0550+";

//        Properties props = new Properties();
//        props.setProperty("mail.store.protocol", "imap");
//        props.setProperty("mail.imap.host", "imap.qiye.aliyun.com");
//        props.setProperty("mail.imap.port", "993");
//        props.setProperty("mail.imap.ssl.enable", "true");
//
//        Session session = Session.getInstance(props);
//        Store store = session.getStore("imap");
//        store.connect("yue05@qhqy.xyz", "chuzhou0550+");
//
//        Folder inbox = store.getFolder("INBOX");
//        inbox.open(Folder.READ_ONLY);
//        log.info("总邮件数：" + inbox.getMessageCount());
//        // 得到收件箱文件夹信息，获取邮件列表
//        log.info("未读邮件数：" + inbox.getUnreadMessageCount());
//        Message[] messages = inbox.getMessages();
//        for (Message message : messages) {
//            String subject = message.getSubject();
//            Object content = message.getContent();
//            System.out.println("Subject: " + subject);
//            System.out.println("Content: " + StrUtil.toString(content));
//        }
//
//        inbox.close(false);
//        store.close();

        Properties props = new Properties();
        props.setProperty("mail.store.protocol", "imap");
        props.setProperty("mail.imap.host", "imap.qq.com");
        props.setProperty("mail.imap.port", "993");
        props.setProperty("mail.imap.ssl.enable", "true");

        Session session = Session.getInstance(props);
        Store store = session.getStore("imap");
        store.connect("2542399294@qq.com", "lpusheckmxumdiah");

        Folder inbox = store.getFolder("INBOX");
        inbox.open(Folder.READ_ONLY);
        log.info("总邮件数：" + inbox.getMessageCount());
        // 得到收件箱文件夹信息，获取邮件列表
        log.info("未读邮件数：" + inbox.getUnreadMessageCount());
        Message[] messages = inbox.getMessages();
        for (Message message : messages) {
            String subject = message.getSubject();
            Object content = message.getContent();
            System.out.println("Subject: " + subject);
            System.out.println("Content: " + StrUtil.toString(content));
        }

        inbox.close(false);
        store.close();
    }
}
